﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

/// <summary>
/// GoogleApiHelper 的摘要描述
/// </summary>
public class GoogleApiHelper
{
    public static List<string> GetLatLng(string address)
    {
        var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=true", Uri.EscapeDataString(address));

        var xdoc = XDocument.Load(requestUri);
        var latLngArray = new List<string>();
        var xElement = xdoc.Element("GeocodeResponse");
        if (xElement != null)
        {
            var result = xElement.Element("result");
            if (result != null)
            {
                var element = result.Element("geometry");
                if (element != null)
                {
                    var locationElement = element.Element("location");
                    if (locationElement != null)
                    {
                        var xElement1 = locationElement.Element("lat");
                        if (xElement1 != null)
                        {
                            var lat = xElement1.Value;
                            latLngArray.Add(lat);

                        }
                        var element1 = locationElement.Element("lng");
                        if (element1 != null)
                        {
                            var lng = element1.Value;
                            latLngArray.Add(lng);
                        }
                    }
                }
            }
        }
        return latLngArray;
    }
}